package com.yanqu.road.server.cmd.zhaoshang;

import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.logic.cross.CrossGroupWeekRankListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.zhaoshang.ZhaoShangProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.zhaoshang.ZsRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_ZHAO_SHANG_PROJECT_RANK_LIST, desc = "排行榜")
public class ZsWeekRankListCmd extends CrossCommand {


    private int rankType;

    private int weekId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhaoShangProto.CrossZsWeekRankListReqMsg reqMsg = ZhaoShangProto.CrossZsWeekRankListReqMsg.parseFrom(packet.getBytes());
        rankType = reqMsg.getRankType();
        weekId = reqMsg.getWeekId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int groupId = ZsRankMgr.getGroupId(serverId);
        long userId = packet.getPlayerId();
        CrossGroupWeekRankListModel rankListModel = ZsRankMgr.getCrossGroupWeekRankListModel(groupId, weekId, eBigRankType.Normal.getValue(), rankType);
        ZhaoShangProto.ZsWeekRankListRespMsgFromCross.Builder respMsg = ZhaoShangProto.ZsWeekRankListRespMsgFromCross.newBuilder();
        respMsg.setRankType(rankType);
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        if(rankListModel != null){
            RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();
            respMsg.addAllRankList(rankListMsg.getRankListList());
            int rank = 1;
            for(RankProto.RankTempMsg rankTempMsg : rankListMsg.getRankListList()) {
                if(rankTempMsg.getUserId() == packet.getPlayerId() && rankTempMsg.getPlayerBaseData().getServerId() == serverId){
                    respMsg.setValue(rankTempMsg.getValue());
                    respMsg.setMyRank(rank);
                    break;
                }
                rank++;
            }
            if(respMsg.getMyRank() > 0){
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_RANK_LIST, respMsg);
                MessageHelper.sendPacket(serverId, userId, pbMsg);
            }else{
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_ZS_PROJECT_RANK_LIST_FROM_CROSS, respMsg);
                MessageHelper.sendPacket(serverId, userId, pbMsg);
            }
        }else{
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_ZS_PROJECT_RANK_LIST, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }
}
